package com.example.springbootquartz.service.impl;

import com.example.springbootquartz.job.BaseJob;
import com.example.springbootquartz.mapper.JobMapper;
import com.example.springbootquartz.service.JobService;
import com.example.springbootquartz.vo.JobInfo;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author zhanpengguo
 * @date 2019-08-21 17:00
 */
@Service
public class JobServiceImpl implements JobService {

    @Autowired
    @Qualifier("Scheduler")
    Scheduler scheduler;

    @Autowired
    JobMapper jobMapper;

    @Override
    public void addJob(JobInfo jobInfo) throws Exception{
        addCronJob(jobInfo);
    }

    @Override
    public void pauseJob(JobInfo jobInfo) throws Exception {
        scheduler.pauseJob(JobKey.jobKey(jobInfo.getJobClassName(), jobInfo.getJobGroupName()));
    }

    @Override
    public void resumeJob(JobInfo jobInfo) throws Exception {
        scheduler.resumeJob(JobKey.jobKey(jobInfo.getJobClassName(), jobInfo.getJobGroupName()));
    }

    @Override
    public void rescheduleJob(JobInfo jobInfo) throws Exception {
        TriggerKey triggerKey = TriggerKey.triggerKey(jobInfo.getJobClassName(), jobInfo.getJobGroupName());
        CronTrigger trigger = (CronTrigger) scheduler.getTrigger(triggerKey);
        CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule(jobInfo.getCronExpression());
        trigger = trigger.getTriggerBuilder().withIdentity(triggerKey).withSchedule(cronScheduleBuilder).build();
        scheduler.rescheduleJob(triggerKey, trigger);
    }

    @Override
    public void deleteJob(JobInfo jobInfo) throws Exception {
        delete(jobInfo);
    }

    @Override
    public List<JobInfo> queryJob() throws Exception {
        return jobMapper.queryAll();
    }

    private void addCronJob(JobInfo jobInfo) throws Exception{
        //启动调度器
        scheduler.start();
        JobDetail jobDetail = JobBuilder.newJob(getClass(jobInfo.getJobClassName()).getClass())
                .withIdentity(jobInfo.getJobClassName(), jobInfo.getJobGroupName())
                .build();
        CronScheduleBuilder cronScheduleBuilder = CronScheduleBuilder.cronSchedule(jobInfo.getCronExpression());
        CronTrigger cronTrigger = TriggerBuilder.newTrigger()
                .withIdentity(jobInfo.getJobClassName(), jobInfo.getJobGroupName())
                .withSchedule(cronScheduleBuilder)
                .build();
        scheduler.scheduleJob(jobDetail, cronTrigger);
    }

    private void delete(JobInfo jobInfo)throws Exception {
        String jobClassName = jobInfo.getJobClassName();
        String jobGroupName = jobInfo.getJobGroupName();
        //暂停
        scheduler.pauseJob(JobKey.jobKey(jobClassName, jobGroupName));
        //停止
        scheduler.unscheduleJob(TriggerKey.triggerKey(jobClassName,jobGroupName));
        //删除
        scheduler.deleteJob(JobKey.jobKey(jobClassName, jobGroupName));
    }

    private BaseJob getClass(String classname) throws Exception {
        return (BaseJob) Class.forName(classname).newInstance();
    }
}
